package com.syh.jn.scm.dto.support;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * Created by soci.song on 2019/1/18.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HistoryDto implements Serializable {

    @NotNull(message = "businessEntityId不能为空")
    private Long businessEntityId;

    @NotNull(message="accountCompanyId不能为空")
    private Long accountCompanyId;

    private String accountCompanyName;

    @NotNull(message = "accountId不能为空")
    private Long accountId;

    @NotNull(message = "account不能为空")
    private String account;

    @NotNull(message = "accountType不能为空")
    private String accountType;

    private String accountName;

    @NotNull(message = "ip不能为空")
    private String ip;

    @NotNull(message = "type不能为空")
    private Short type;

    @NotNull(message = "originalId不能为空")
    private Long originalId;

    @NotNull(message = "originalCode不能为空")
    private String originalCode;

    @NotNull(message = "history不能为空")
    private String history;

    public HistoryDto(JSONObject json,String ip){
        this.businessEntityId = json.getLong("supplierId") == null ? json.getLong("businessEntityId") :json.getLong("supplierId");
        this.accountCompanyId = json.getLong("businessEntityId");
        this.accountCompanyName = json.getString("businessEntityName");
        this.accountId = json.getLong("accountId");
        this.account = json.getString("loginAccount");
        this.accountName = json.getString("accountName");
        this.accountType = json.getString("accountType");
        this.ip = ip;
    }

    public void buildBusinessEntity(JSONObject json){
        this.originalCode = json.getString("code");
        this.history = json.toString();
    }

    public void buildHistory(JSONObject json){
        if (json == null ) {
            this.originalCode = "";
            this.history = "";
            return;
        }
        this.originalCode = json.getString("code");
        this.history = json.toString();
    }
}
